﻿<?php
require 'test_header.php';


class test_class_user_signup_process extends UnitTestCase{

	function setUp() {
		$db = db_connect();
		$sql = "
CREATE TABLE IF NOT EXISTS `user` (
  `username` varchar(255) NOT NULL,
  `passwd` varchar(128) NOT NULL,
  `role` varchar(16) NOT NULL,
   `email` varchar(16) NOT NULL,
  `signup_time` varchar(64) DEFAULT NULL,
  `last_signin_time` varchar(64) DEFAULT NULL,
  `last_signin_ipadd` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
		$res = $db->query( $sql );
	
		$sql ="
CREATE TABLE IF NOT EXISTS `user_profile` (
  `username` varchar(255) NOT NULL,
  `nick` varchar(255) DEFAULT NULL,
  `sex` varchar(6) DEFAULT NULL,
  `location` varchar(1024) DEFAULT NULL,
  `motto` varchar(1024) DEFAULT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
$res = $db->query( $sql );

	$sql = "
CREATE TABLE IF NOT EXISTS `user_judge_statistics` (
  `username` varchar(256) NOT NULL,
  `submit_times` bigint(20) NOT NULL DEFAULT  '0',
  `accepted_times` bigint(20) NOT NULL DEFAULT  '0',
  `wrong_answer_times` bigint(20) NOT NULL DEFAULT  '0',
  `presentation_times` bigint(20) NOT NULL DEFAULT  '0',
  `compilation_error_times` bigint(20) NOT NULL DEFAULT  '0',
  `time_exceeded_times` bigint(20) NOT NULL DEFAULT  '0',
  `memory_exceeded_times` bigint(20) NOT NULL DEFAULT  '0',
  `output_exceeded_times` bigint(20) NOT NULL DEFAULT  '0',
  `runtime_error_times` bigint(20) NOT NULL DEFAULT  '0',
  `compile_time_over` bigint(20) NOT NULL DEFAULT  '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
$res = $db->query( $sql );

	}

	function tearDown(){ 
		
		$db = db_connect();
		$sql = 'DROP TABLE `user`';
		$res = $db->query( $sql );
		$sql = 'DROP TABLE `user_profile`' ;
		$res = $db->query( $sql );
		$sql = 'DROP TABLE `user_judge_statistics`' ;
		$res = $db->query( $sql );
	}

	function test_singup_new_user(){

		$db = db_connect();
			
		$request = array( 'username' => rand() , 'passwd' => rand() );
	
		$user_signup_process = new user_signup_process( $db , $request );
		
		$this->assertTrue( $user_signup_process->do_signup() );

		$db_obj_user = new db_obj_user( db_connect() );
		//验证刚存入数据库中的用户是否可以被顺利读出
		$this->assertTrue( $db_obj_user->load( 'username' , $request['username'] ) );
		$this->assertEqual( $db_obj_user->get( 'passwd') , md5($request['passwd'] ) );

		//断言用户详细信息表中该用户的相应记录已经被建立
		$sql = "select * from user_profile where username = ".$request['username'];
		$this->assertTrue( $db->query( $sql ) );

		//断言用户提交记录表中该用户的相应记录已经被建立
		$sql = "select * from user_judge_statistics where username = ".$request['username'];
		$this->assertTrue( $db->query( $sql ) );
	
		//而未存入的用户读出是将会有异常抛出
		$this->expectException();
		$db_obj_user->load( 'username' , 'rand_is_cound`t_equal_me' );
	}
 
	function test_delete_user(){

		$db = db_connect();
		$request = array( 'username' => rand() , 'passwd' => rand() );
		$user_signup_process = new user_signup_process( $db , $request );
	
		$this->assertTrue( $user_signup_process->do_signup() );

		$this->assertTrue( user_signup_process::delete_user( $db , $request['username'] ) );
		$db_obj_user = new db_obj_user( $db ) ;

		$this->expectException();
		$db_obj_user->load( 'username' , 'judas' ) ;
	}
}

$test = new test_class_user_signup_process();
$test->run( new HtmlReporter() );
